<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetLanguages;

use App\Base\Controllers\BaseAdminController;
use App\Plugins\FcpApi\Base\TweetBaseController;
use Illuminate\Http\Request;

class FsControllerAdmin extends TweetBaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->service = new FsService();
    }

    //配置新增多语言
    public function configStore(Request $request)
    {
        $tableName = $request->input('table_name');
        $tableField = $request->input('table_field');
        $tableKey = $request->input('table_key');
        $langJson = $request->input('lang_json');
        $langArr = json_decode($langJson, true);
        FsModel::where('table_key', $tableKey)->delete();
        $itemArr = [];
        foreach ($langArr as $v) {
            $item = [];
            $item['table_name'] = $tableName;
            $item['table_field'] = $tableField;
            $item['table_key'] = $tableKey;
            $item['lang_tag'] = $v['lang_tag'];
            $item['lang_content'] = $v['lang_content'];
            $itemArr[] = $item;
        }

        FsModel::insert($itemArr);

        $this->success();
    }

    // 验证规则
    public function rules($ruleType)
    {
        $rule = [];

        $config = new FsConfig($this->service->getTable());

        switch ($ruleType) {
            case FsConfig::RULE_STORE:
                $rule = $config->storeRule();
                break;

            case FsConfig::RULE_UPDATE:
                $rule = $config->updateRule();
                break;

            case FsConfig::RULE_DESTROY:
                $rule = $config->destroyRule();
                break;

            case FsConfig::RULE_DETAIL:
                $rule = $config->detailRule();
                break;
        }

        return $rule;
    }
}
